package com.huaxin.webexam.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.huaxin.webexam.bean.Admin;
import com.huaxin.webexam.bean.Student;
import com.huaxin.webexam.bean.Teacher;
import com.huaxin.webexam.dao.AdminDao;
import com.huaxin.webexam.utils.JdbcUtil;

public class AdminDaoImpl implements AdminDao {

	@Override
	public List<Admin> login(Admin admin) {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner();
		String sql = "select * from  tb_admin where aname=? and apassword=?";
		try {
			java.sql.Connection connection = JdbcUtil.getConn();
			List<Admin> query = queryRunner.query(connection, sql, new BeanListHandler<>(Admin.class), admin.getAname(),
					admin.getApassword());
			connection.close();
			return query;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Student> StudentList() {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner();
		String sql = "select * from  tb_student";
		try {
			java.sql.Connection connection = JdbcUtil.getConn();
			List<Student> query = queryRunner.query(connection, sql, new BeanListHandler<>(Student.class));
			connection.close();
			return query;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void deleteStudent(Student student) {
		// TODO Auto-generated method stub
		deleteStudentErrors(student);
		QueryRunner queryRunner = new QueryRunner();
		String sql = "delete from tb_student where sno=?";
		java.sql.Connection connection = null;
		try {
			connection = JdbcUtil.getConn();
			queryRunner.execute(connection, sql, new BeanListHandler<>(Student.class), student.getSno());
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public List<Teacher> TeacherList() {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner();
		String sql = "select * from  tb_teacher";
		try {
			java.sql.Connection connection = JdbcUtil.getConn();
			List<Teacher> query = queryRunner.query(connection, sql, new BeanListHandler<>(Teacher.class));
			connection.close();
			return query;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void deleteTeacher(Teacher teacher) {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner();
		String sql = "delete from tb_teacher where  tno=?";
		java.sql.Connection connection = null;
		try {
			connection = JdbcUtil.getConn();
			queryRunner.execute(connection, sql, new BeanListHandler<>(Teacher.class), teacher.getTno());
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 表 student 外键errors sno
	public void deleteStudentErrors(Student student) {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner();
		String sql = "delete from tb_errors where sno=?";
		java.sql.Connection connection = null;
		try {
			connection = JdbcUtil.getConn();
			queryRunner.execute(connection, sql, new BeanListHandler<>(Student.class), student.getSno());
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
